#include "main_menu.h"


MAIN_MENU::MAIN_MENU(CASH *cash):QObject(), QGraphicsItemGroup()
{

    this->cash=cash;

    sound = new SOUND_POWER(cash,"Res/Sound/Other/menu.ogg",this);

    this->setFlag(ItemIsFocusable, true);
    unvis = true;

    init();

    current_focus = 0;

    connect(tl_items_show, SIGNAL(finished()), this, SLOT(slot_current_focus()));

}

void MAIN_MENU::init()
{
    lst_items = new QList<MENU_ITEM*>();
    tl_items_show = new QTimeLine(1000, this);
    tl_main_show = new QTimeLine(1000, this);

    MENU_ITEM *item = new MENU_ITEM(MENU_ITEM_TYPE(STORY), tl_items_show,cash);
    lst_items->append(item);
    this->addToGroup(item);
    item->setZValue(1);

    item = new MENU_ITEM(MENU_ITEM_TYPE(BRUTAL), tl_items_show,cash);
    lst_items->append(item);
    this->addToGroup(item);
    item->setZValue(1);

    item = new MENU_ITEM(MENU_ITEM_TYPE(QUIT), tl_items_show,cash);
    lst_items->append(item);
    this->addToGroup(item);
    item->setZValue(1);

    item_main = new MENU_ITEM(MAIN, tl_main_show,cash);
    this->addToGroup(item_main);
    item_main->setZValue(1);

}

void MAIN_MENU::keyPressEvent(QKeyEvent *event)
{
    switch (event->key()) {

    case Qt::Key_W:
    case Qt::Key_Up:

        current_focus--;

        if(current_focus < 0){
            lst_items->at(0)->set_focus(false);

            current_focus = (lst_items->count() - 1);
            lst_items->at(current_focus)->set_focus(true);

            break;
        }

        for(int i=0;i<lst_items->count();i++){
            if(i == current_focus){
                lst_items->at(i)->set_focus(true);
                lst_items->at(i + 1)->set_focus(false);

                break;
            }
        }

        break;

    case Qt::Key_S:
    case Qt::Key_Down:

        current_focus++;

        for(int i=0;i<lst_items->count();i++){
            if(current_focus > (lst_items->count() - 1)){
                lst_items->at(lst_items->count() - 1)->set_focus(false);

                current_focus = 0;
                lst_items->at(current_focus)->set_focus(true);

                break;
            }

            if(i == current_focus){
                lst_items->at(i)->set_focus(true);
                lst_items->at(i - 1)->set_focus(false);

                break;
            }
        }

        break;

    case Qt::Key_Escape:
        emit signal_menu_closed();
        break;

    case Qt::Key_Return:
    case Qt::Key_Enter:
        set_menu_action();
        break;

    default:
        break;
    }
}

QPainterPath MAIN_MENU::shape() const
{
    return QPainterPath();
}

void MAIN_MENU::show()
{

    tl_main_show->stop();
    tl_main_show->setDirection(QTimeLine::Forward);
    tl_main_show->start();

    tl_items_show->stop();
    tl_items_show->setDirection(QTimeLine::Forward);
    tl_items_show->start();

    this->unvis = false;
}

void MAIN_MENU::hide()
{
    lst_items->at(current_focus)->set_focus(false);

    tl_main_show->stop();
    tl_main_show->setDirection(QTimeLine::Backward);
    tl_main_show->start();

    connect(lst_items->at(current_focus)->Get_tl_hover(), SIGNAL(finished()), this, SLOT(slot_menu_closed()));
}

//функция исполняет выбранный пункт меню
void MAIN_MENU::set_menu_action()
{
    switch (lst_items->at(current_focus)->Get_item_type()) {

    case STORY:
        emit signal_menu_item_pressed(STORY);
        sound->Play_Now();
        break;

    case BRUTAL:
        emit signal_menu_item_pressed(BRUTAL);
        sound->Play_Now();
        break;

    case ENEMIES:
        emit signal_menu_item_pressed(ENEMIES);
        sound->Play_Now();
        break;

    case SETTINGS:
        emit signal_menu_item_pressed(SETTINGS);
        sound->Play_Now();
        break;

    case QUIT:
        emit signal_menu_item_pressed(QUIT);
        sound->Play_Now();
        break;

    default:
        break;
    }
}

bool MAIN_MENU::is_unvis()
{
    return unvis;
}

MAIN_MENU::~MAIN_MENU(){}

// слот срабатывает при включении меню
void MAIN_MENU::slot_current_focus()
{
    if(!unvis){
        lst_items->at(current_focus)->set_focus(true);
    }
}

//слот срабатывает при выключении меню
void MAIN_MENU::slot_menu_closed()
{
    disconnect(lst_items->at(current_focus)->Get_tl_hover(), SIGNAL(finished()), this, SLOT(slot_menu_closed()));

    tl_items_show->stop();
    tl_items_show->setDirection(QTimeLine::Backward);
    tl_items_show->start();

    this->unvis = true;
}
